﻿using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.Core.Domain.EntityBase;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Requests;
using ZilLion.Service.ZilLionOss.Domain.Responses;
using ZilLion.Service.ZilLionOss.Domain.Services.Interface;

namespace ZilLion.Service.ZilLionOss.Controllers
{
    /// <summary>
    ///     租户设置
    /// </summary>
    [Route("Api/OssBack/Compnay")]
    public class OssCompnayController : OssControllerBase
    {
        private readonly IOssCompanyService _ossCompanyService;
        private readonly IOssRouteService _ossRouteService;

        public OssCompnayController(IOssCompanyService ossCompanyService, IOssRouteService ossRouteService)
        {
            _ossCompanyService = ossCompanyService;
            _ossRouteService = ossRouteService;
        }

        /// <summary>
        ///     获取全部租户
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetAllCompanys")]
        [HttpGet]
        public CompanyListResponse GetAllCompanys(string token = null)
        {
            var currentCompanykey = GetZlCompanyKey();
            if (currentCompanykey.ToLower() != "zillion") //非纵联人员不允许使用
                return new CompanyListResponse
                {
                    Errormodel = new WebErrorModel {ErrDetail = "无操作权限!", ErrMessage = "无操作权限!", Errtype = "FaildAuth"}
                };

            var list = _ossCompanyService.GetAllCompanys();
            return new CompanyListResponse {Companys = list};
        }

        /// <summary>
        ///     使用key 获取单个租户信息
        /// </summary>
        /// <param name="companykey"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetCompany")]
        [HttpGet]
        public CompanyResponse GetCompany(string companykey, string token = null)
        {
            var currentCompanykey = GetZlCompanyKey();
            if (currentCompanykey.ToLower() != "zillion") //非纵联人员不允许使用
                return new CompanyResponse
                {
                    Errormodel = new WebErrorModel {ErrDetail = "无操作权限!", ErrMessage = "无操作权限!", Errtype = "FaildAuth"}
                };
            var info = _ossCompanyService.GetCompany(companykey);
            return new CompanyResponse {Company = info};
        }

        /// <summary>
        /// 设置租户可用路由
        /// </summary>
        /// <param name="companykey"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetCompanyRoutes")]
        [HttpGet]
        public CompnayRoutesResponse GetCompanyRoutes(string companykey, string token = null)
        {
            var list =_ossCompanyService.GetCompanyRoutes(companykey);
            return new CompnayRoutesResponse(){Routes = list.ToArray() };
        }

        /// <summary>
        /// 设置租户可用路由
        /// </summary>
        /// <param name="request"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SetCompanyRoutes")]
        [HttpPost]
        public ResponseBase SetCompanyRoutes([FromBody] CompanyRoutesSetRequest request, string token = null)
        {
            var currentCompanykey = GetZlCompanyKey();
            var userid = GetUserId();
            if (currentCompanykey.ToLower() != "zillion") //非纵联人员不允许使用
                return new ResponseBase
                {
                    Errormodel = new WebErrorModel {ErrDetail = "无操作权限!", ErrMessage = "无操作权限!", Errtype = "FaildAuth"}
                };
            var list = request.Routepaths.Select(route => new OssCompanyRouteEntity
                {
                    ZlcompanyKey = request.Companykey,
                    RoutePath = route,
                    UpdateId = userid,
                    UpdateTime = DateTime.Now.GetNowChinaTime()
                })
                .ToList();
            _ossCompanyService.SetCompanyRoutes(list);
            return new ResponseBase();
        }

        /// <summary>
        ///     保存租户信息
        /// </summary>
        /// <param name="companyinfo"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("SaveCompany")]
        [HttpPost]
        public CompanyResponse SaveCompany([FromBody] OssCompanyDto companyinfo, string token = null)
        {
            var currentCompanykey = GetZlCompanyKey();
            companyinfo.UpdateId = GetUserId();
            if (currentCompanykey.ToLower() != "zillion") //非纵联人员不允许使用
                return new CompanyResponse
                {
                    Errormodel = new WebErrorModel {ErrDetail = "无操作权限!", ErrMessage = "无操作权限!", Errtype = "FaildAuth"}
                };
            var info = _ossCompanyService.SaveCompany(companyinfo);
            return new CompanyResponse {Company = info};
        }

        /// <summary>
        ///     删除租户信息
        /// </summary>
        /// <param name="companykey"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("RemoveCompany")]
        [HttpDelete]
        public ResponseBase RemoveCompany(string companykey, string token = null)
        {
            var currentCompanykey = GetZlCompanyKey();
            if (currentCompanykey.ToLower() != "zillion") //非纵联人员不允许使用
                return new ResponseBase
                {
                    Errormodel = new WebErrorModel {ErrDetail = "无操作权限!", ErrMessage = "无操作权限!", Errtype = "FaildAuth"}
                };
            _ossCompanyService.RemoveCompany(companykey);
            return new ResponseBase();
        }
    }
}